import numpy as np
#激活函数
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
def sigmoid_grad(x):
    return (1.0 - sigmoid(x)) * sigmoid(x)
def softmax(x):
    if x.ndim==2:
        x=x-np.max(x,axis=1,keepdims=True)
        y=np.exp(x)/np.sum(np.exp(x),axis=1,keepdims=True)
        return y
    x=x-np.max(x)
    return np.exp(x)/np.sum(np.exp(x))
#误差函数
def mean_squared_error(y, t):#均方误差
    return 0.5 * np.sum(np.square(y - t))
#交叉熵误差
def cross_entropy_error(y,t):
    delta=1e-7
    if y.ndim==1:
        y=y.reshape(-1,y.size)
        t=t.reshape(-1,t.size)
    batch_size=y.shape[0]
    return -np.sum(t*np.log(y+delta))/batch_size

